package com.haidechizi.backup.tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * @author: wangdejun
 * @create: 2020-03-14 11:42
 **/
public class Node {
    private int value;

    private Node left;
    private Node right;

    public Node(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }


    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    public Node searchNode(int i) {
        Node node = null;
        if (this.value == i) {
            return this;
        } else {
            if (this.left != null) {
                node = this.left.searchNode(i);
            }
            if (node != null) {
                return node;
            }
            if (this.right != null) {
                node = this.right.searchNode(i);
            }
        }


        return node;
    }

    public void frontPrint() {
        System.out.println(this.value);
        if (this.left != null) {
            this.left.frontPrint();

        }
        if (this.right != null) {
            this.right.frontPrint();

        }
    }

    public void midPrint() {
        if (this.left != null) {
            this.left.midPrint();
        }
        System.out.println(this.value);
        if (this.right != null) {
            this.right.midPrint();
        }
    }

    public void afterPrint() {
        if (this.left != null) {
            this.left.afterPrint();
        }
        if (this.right != null) {
            this.right.afterPrint();
        }
        System.out.println(this.value);
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                ", left=" + left +
                ", right=" + right +
                '}';
    }

    public void delete(int i) {
        Node parent = this;
        if (this.left != null && this.left.value == i) {
            this.left = null;
        }
        if (this.right != null && this.right.value == i) {
            this.right = null;
        }
        if (this.left != null) {
            this.left.delete(i);
        }
        if (this.right != null) {
            this.right.delete(i);
        }
    }

    public void deepShow() {
        Queue<Node> queue = new LinkedList<>();
        queue.add(this);
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            System.out.println(node.value);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
    }
}
